<?php
require_once 'Datatype_string.php';

/**
 * Datatype for password
 * 
 * @package EDO
 * @subpackage Datatypes
 *
 */
class Datatype_password extends Datatype_string 
{
	protected $_SQLValueIsQuoted = true;	
	
	public function validate($validate)
	{
		if(is_array($validate))
		{
			if(isset($validate['new1']) AND isset($validate['new2']))
			{
				if($validate['new1'] AND $validate['new2'])
				{
					if(parent::validate($validate['new1']) !== false)
					{
						if($validate['new1'] == $validate['new2'])			
						{
							 if(strlen($validate['new1']) < 4)
							 {
							     return false;
							     
							 }
							     
						    
						    $validate = md5($validate['new1']);
						}
						else 
						{
							$validate = false;
						}
					}
					else 
					{
						$validate = false;
					}
				}
				else 
				{
					if(!$validate['new1'] AND !$validate['new2'])
						$validate = $validate['old'];
					else 
						$validate = false;
				}
			}
			elseif(!isset($validate['new1']) AND !isset($validate['new2'])) 
			{
				$validate = $validate['old'];
			}
			else 
			{
				$validate = false;
			}
		}
		else 
		{
			$validate = parent::validate($validate);
		}

		
		if($validate !== false)
		{
		    if(is_array($validate))
		         return false;
		    
		    if(strlen($validate) < 4)
		          return false;
		}
		
		
		
		
		return $validate;	
	}
		
	
	public function getDisplayValue()
	{
		return str_repeat("*", strlen($this->value));
	}
	
	
	public function forceValue()
	{
		return false;
	}
	
	
	
	public function randomize($length = 6)
	{
		
		LEAD::requirePackage('Security/PasswordUtility');
		
		$newPwd = PasswordUtility::createPassword($length);
		
		$this->setValue(array('new1' => $newPwd, 'new2' => $newPwd));
		return $newPwd;
	}
		
		
		
	
}
?>